package com.yyw.foodie.controller.base;


import com.yyw.foodie.bo.orders.SubmitOrderBo;
import com.yyw.foodie.common.Result;
import com.yyw.foodie.enums.OrderEnums;
import com.yyw.foodie.pojo.OrderStatusEntity;
import com.yyw.foodie.service.OrderStatusService;
import com.yyw.foodie.service.OrdersService;
import com.yyw.foodie.utils.BindingResultJoinError;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 订单表; 前端控制器
 * </p>
 *
 * @author yiyw
 * @since 2021-05-22
 */
@Slf4j
@RestController
@RequestMapping("/orders")
@Api(value = "订单相关", tags = "订单相关控制器")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderStatusService orderStatusService;

    @PostMapping("/create")
    @ApiOperation(value = "创建订单", notes = "创建订单信息")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result createOrder(@RequestBody @Valid SubmitOrderBo submitOrderBo, BindingResult bindingResult,
                              HttpServletRequest request, HttpServletResponse response) {
        // 0. 校验数据
        Map<String, String> map = BindingResultJoinError.joinErrorMsg(bindingResult);
        if (map != null) {
            return Result.errorMap(map);
        }

        // 1. 创建订单
        log.info("即将创建订单: {}", submitOrderBo.toString());
        long start = System.currentTimeMillis();
        String orderId = ordersService.createOrder(submitOrderBo);
        long end = System.currentTimeMillis();
        log.info("订单创建成功,耗时: {}", end - start);

        // 2. 删除购物车相应的商品
        ordersService.removeCatBySku(submitOrderBo.getItemSpecIds(), submitOrderBo.getUserId(), request, response);
        // 3. 支付
        return Result.ok(orderId);
    }


    /**
     * 聚合支付回调我们的方法
     * 告诉我们哪笔订单支付成功了
     * @param merchantOrderId
     * @return
     */
    @PostMapping("noticePaySuccess")
    @ApiOperation(value = "支付成功的回调方法", notes = "商户端支付成功回调方法")
    public Integer noticePaySuccess(@ApiParam(value = "商户客户端的订单ID") @RequestParam(value = "merchantOrderId") String merchantOrderId) {
        orderStatusService.updateOrderStatus(merchantOrderId, OrderEnums.已付款待发货.code);
        return HttpStatus.OK.value();
    }


    /**
     * 前台支付页，轮询获取支付结果
     * @param orderId   订单ID
     * @return
     */
    @PostMapping("/getPaidOrderInfo")
    @ApiOperation(value = "轮询获取支付结果", notes = "轮询获取支付结果")
    public Result getPaidOrderInfo(@ApiParam(value = "订单ID") @RequestParam(value = "orderId") String orderId) {
        OrderStatusEntity entity = orderStatusService.getById(orderId);
        return Result.ok(entity);
    }

}

